
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.defects.corrections &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.defects.corrections</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.defects.corrections</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Implementation of defect correction methods.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy</span>
<span class="kn">from</span> <span class="nn">scipy</span> <span class="kn">import</span> <span class="n">stats</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.defects.core</span> <span class="kn">import</span> <span class="n">DefectCorrection</span>
<span class="kn">from</span> <span class="nn">pymatgen.analysis.defects.utils</span> <span class="kn">import</span> <span class="n">ang_to_bohr</span><span class="p">,</span> <span class="n">hart_to_ev</span><span class="p">,</span> <span class="n">eV_to_k</span><span class="p">,</span> \
    <span class="n">generate_reciprocal_vectors_squared</span><span class="p">,</span> <span class="n">QModel</span><span class="p">,</span> <span class="n">converge</span><span class="p">,</span> <span class="n">tune_for_gamma</span><span class="p">,</span> \
    <span class="n">generate_R_and_G_vecs</span><span class="p">,</span> <span class="n">kumagai_to_V</span>

<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Danny Broberg, Shyam Dwaraknath&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2018, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;1.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyam Dwaraknath&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyamd@lbl.gov&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Development&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;Mar 15, 2018&quot;</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="FreysoldtCorrection"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.FreysoldtCorrection">[docs]</a><span class="k">class</span> <span class="nc">FreysoldtCorrection</span><span class="p">(</span><span class="n">DefectCorrection</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class for FreysoldtCorrection class. Largely adapated from PyCDT code</span>

<span class="sd">    If this correction is used, please reference Freysoldt&#39;s original paper.</span>
<span class="sd">    doi: 10.1103/PhysRevLett.102.016402</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dielectric_const</span><span class="p">,</span> <span class="n">q_model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">energy_cutoff</span><span class="o">=</span><span class="mi">520</span><span class="p">,</span> <span class="n">madetol</span><span class="o">=</span><span class="mf">0.0001</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes the FreysoldtCorrection class</span>
<span class="sd">        Args:</span>
<span class="sd">            dielectric_const (float or 3x3 matrix): Dielectric constant for the structure</span>
<span class="sd">            q_model (QModel): instantiated QModel object or None.</span>
<span class="sd">                Uses default parameters to instantiate QModel if None supplied</span>
<span class="sd">            energy_cutoff (int): Maximum energy in eV in reciprocal space to perform</span>
<span class="sd">                integration for potential correction.</span>
<span class="sd">            madeltol(float): Convergence criteria for the Madelung energy for potential correction</span>
<span class="sd">            axis (int): Axis to calculate correction.</span>
<span class="sd">                If axis is None, then averages over all three axes is performed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">q_model</span> <span class="o">=</span> <span class="n">QModel</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">q_model</span> <span class="k">else</span> <span class="n">q_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">energy_cutoff</span> <span class="o">=</span> <span class="n">energy_cutoff</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">madetol</span> <span class="o">=</span> <span class="n">madetol</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dielectric_const</span> <span class="o">=</span> <span class="n">dielectric_const</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dielectric_const</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">or</span> \
                <span class="nb">isinstance</span><span class="p">(</span><span class="n">dielectric_const</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">dielectric_const</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">diag</span><span class="p">(</span><span class="n">dielectric_const</span><span class="p">)))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="o">=</span> <span class="n">axis</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;pot_plot_data&quot;</span><span class="p">:</span> <span class="p">{},</span> <span class="s2">&quot;pot_corr_uncertainty_md&quot;</span><span class="p">:</span> <span class="p">{}}</span>

<div class="viewcode-block" id="FreysoldtCorrection.get_correction"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.FreysoldtCorrection.get_correction">[docs]</a>    <span class="k">def</span> <span class="nf">get_correction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the Freysoldt correction for a defect entry</span>
<span class="sd">        Args:</span>
<span class="sd">            entry (DefectEntry): defect entry to compute Freysoldt correction on.</span>

<span class="sd">                Requires following keys to exist in DefectEntry.parameters dict:</span>

<span class="sd">                    axis_grid (3 x NGX where NGX is the length of the NGX grid</span>
<span class="sd">                    in the x,y and z axis directions. Same length as planar</span>
<span class="sd">                    average lists):</span>
<span class="sd">                        A list of 3 numpy arrays which contain the cartesian axis</span>
<span class="sd">                        values (in angstroms) that correspond to each planar avg</span>
<span class="sd">                        potential supplied.</span>

<span class="sd">                    bulk_planar_averages (3 x NGX where NGX is the length of</span>
<span class="sd">                    the NGX grid in the x,y and z axis directions.):</span>
<span class="sd">                        A list of 3 numpy arrays which contain the planar averaged</span>
<span class="sd">                        electrostatic potential for the bulk supercell.</span>

<span class="sd">                    defect_planar_averages (3 x NGX where NGX is the length of</span>
<span class="sd">                    the NGX grid in the x,y and z axis directions.):</span>
<span class="sd">                        A list of 3 numpy arrays which contain the planar averaged</span>
<span class="sd">                        electrostatic potential for the defective supercell.</span>

<span class="sd">                    initial_defect_structure (Structure) structure corresponding to</span>
<span class="sd">                        initial defect supercell structure (uses Lattice for charge correction)</span>

<span class="sd">                    defect_frac_sc_coords (3 x 1 array) Fractional co-ordinates of</span>
<span class="sd">                        defect location in supercell structure</span>
<span class="sd">        Returns:</span>
<span class="sd">            FreysoldtCorrection values as a dictionary</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">axis</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">list_axis_grid</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;axis_grid&quot;</span><span class="p">])</span>
            <span class="n">list_bulk_plnr_avg_esp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;bulk_planar_averages&quot;</span><span class="p">])</span>
            <span class="n">list_defect_plnr_avg_esp</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;defect_planar_averages&quot;</span><span class="p">])</span>
            <span class="n">list_axes</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">list_axis_grid</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">list_axes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">axis</span><span class="p">)</span>
            <span class="n">list_axis_grid</span><span class="p">,</span> <span class="n">list_bulk_plnr_avg_esp</span><span class="p">,</span> <span class="n">list_defect_plnr_avg_esp</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[],</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">ax</span> <span class="ow">in</span> <span class="n">list_axes</span><span class="p">:</span>
                <span class="n">list_axis_grid</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;axis_grid&quot;</span><span class="p">][</span><span class="n">ax</span><span class="p">]))</span>
                <span class="n">list_bulk_plnr_avg_esp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;bulk_planar_averages&quot;</span><span class="p">][</span><span class="n">ax</span><span class="p">]))</span>
                <span class="n">list_defect_plnr_avg_esp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;defect_planar_averages&quot;</span><span class="p">][</span><span class="n">ax</span><span class="p">]))</span>

        <span class="n">lattice</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;initial_defect_structure&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">defect_frac_coords</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;defect_frac_sc_coords&quot;</span><span class="p">]</span>

        <span class="n">q</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">defect</span><span class="o">.</span><span class="n">charge</span>

        <span class="n">es_corr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">perform_es_corr</span><span class="p">(</span><span class="n">lattice</span><span class="p">,</span> <span class="n">entry</span><span class="o">.</span><span class="n">charge</span><span class="p">)</span>

        <span class="n">pot_corr_tracker</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">pureavg</span><span class="p">,</span> <span class="n">defavg</span><span class="p">,</span> <span class="n">axis</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">list_axis_grid</span><span class="p">,</span> <span class="n">list_bulk_plnr_avg_esp</span><span class="p">,</span> <span class="n">list_defect_plnr_avg_esp</span><span class="p">,</span>
                                            <span class="n">list_axes</span><span class="p">):</span>
            <span class="n">tmp_pot_corr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">perform_pot_corr</span><span class="p">(</span>
                <span class="n">x</span><span class="p">,</span> <span class="n">pureavg</span><span class="p">,</span> <span class="n">defavg</span><span class="p">,</span> <span class="n">lattice</span><span class="p">,</span> <span class="n">entry</span><span class="o">.</span><span class="n">charge</span><span class="p">,</span> <span class="n">defect_frac_coords</span><span class="p">,</span>
                <span class="n">axis</span><span class="p">,</span> <span class="n">widthsample</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span>
            <span class="n">pot_corr_tracker</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tmp_pot_corr</span><span class="p">)</span>

        <span class="n">pot_corr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pot_corr_tracker</span><span class="p">)</span>

        <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;freysoldt_meta&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">)</span>
        <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;potalign&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pot_corr</span> <span class="o">/</span> <span class="p">(</span><span class="o">-</span><span class="n">q</span><span class="p">)</span> <span class="k">if</span> <span class="n">q</span> <span class="k">else</span> <span class="mf">0.</span>

        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;freysoldt_electrostatic&quot;</span><span class="p">:</span> <span class="n">es_corr</span><span class="p">,</span> <span class="s2">&quot;freysoldt_potential_alignment&quot;</span><span class="p">:</span> <span class="n">pot_corr</span><span class="p">}</span></div>

<div class="viewcode-block" id="FreysoldtCorrection.perform_es_corr"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.FreysoldtCorrection.perform_es_corr">[docs]</a>    <span class="k">def</span> <span class="nf">perform_es_corr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lattice</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Peform Electrostatic Freysoldt Correction</span>
<span class="sd">        Args:</span>
<span class="sd">            lattice: Pymatgen lattice object</span>
<span class="sd">            q (int): Charge of defect</span>
<span class="sd">            step (float): step size for numerical integration</span>
<span class="sd">        Return:</span>
<span class="sd">            Electrostatic Point Charge contribution to Freysoldt Correction (float)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Running Freysoldt 2011 PC calculation (should be &quot;</span> <span class="s2">&quot;equivalent to sxdefectalign)&quot;</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;defect lattice constants are (in angstroms)&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">))</span>

        <span class="p">[</span><span class="n">a1</span><span class="p">,</span> <span class="n">a2</span><span class="p">,</span> <span class="n">a3</span><span class="p">]</span> <span class="o">=</span> <span class="n">ang_to_bohr</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">lattice</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;In atomic units, lat consts are (in bohr):&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">([</span><span class="n">a1</span><span class="p">,</span> <span class="n">a2</span><span class="p">,</span> <span class="n">a3</span><span class="p">]))</span>
        <span class="n">vol</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">a1</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">a2</span><span class="p">,</span> <span class="n">a3</span><span class="p">))</span>  <span class="c1"># vol in bohr^3</span>

        <span class="k">def</span> <span class="nf">e_iso</span><span class="p">(</span><span class="n">encut</span><span class="p">):</span>
            <span class="n">gcut</span> <span class="o">=</span> <span class="n">eV_to_k</span><span class="p">(</span><span class="n">encut</span><span class="p">)</span>  <span class="c1"># gcut is in units of 1/A</span>
            <span class="k">return</span> <span class="n">scipy</span><span class="o">.</span><span class="n">integrate</span><span class="o">.</span><span class="n">quad</span><span class="p">(</span><span class="k">lambda</span> <span class="n">g</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">q_model</span><span class="o">.</span><span class="n">rho_rec</span><span class="p">(</span><span class="n">g</span> <span class="o">*</span> <span class="n">g</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">gcut</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">q</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span>

        <span class="k">def</span> <span class="nf">e_per</span><span class="p">(</span><span class="n">encut</span><span class="p">):</span>
            <span class="n">eper</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">for</span> <span class="n">g2</span> <span class="ow">in</span> <span class="n">generate_reciprocal_vectors_squared</span><span class="p">(</span><span class="n">a1</span><span class="p">,</span> <span class="n">a2</span><span class="p">,</span> <span class="n">a3</span><span class="p">,</span> <span class="n">encut</span><span class="p">):</span>
                <span class="n">eper</span> <span class="o">+=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">q_model</span><span class="o">.</span><span class="n">rho_rec</span><span class="p">(</span><span class="n">g2</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="n">g2</span>
            <span class="n">eper</span> <span class="o">*=</span> <span class="p">(</span><span class="n">q</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="nb">round</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span> <span class="o">/</span> <span class="n">vol</span>
            <span class="n">eper</span> <span class="o">+=</span> <span class="p">(</span><span class="n">q</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">*</span> <span class="nb">round</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">q_model</span><span class="o">.</span><span class="n">rho_rec_limit0</span> <span class="o">/</span> <span class="n">vol</span>
            <span class="k">return</span> <span class="n">eper</span>

        <span class="n">eiso</span> <span class="o">=</span> <span class="n">converge</span><span class="p">(</span><span class="n">e_iso</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">madetol</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energy_cutoff</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Eisolated : </span><span class="si">%f</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nb">round</span><span class="p">(</span><span class="n">eiso</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>

        <span class="n">eper</span> <span class="o">=</span> <span class="n">converge</span><span class="p">(</span><span class="n">e_per</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">madetol</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">energy_cutoff</span><span class="p">)</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Eperiodic : </span><span class="si">%f</span><span class="s2"> hartree&quot;</span><span class="p">,</span> <span class="nb">round</span><span class="p">(</span><span class="n">eper</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;difference (periodic-iso) is </span><span class="si">%f</span><span class="s2"> hartree&quot;</span><span class="p">,</span> <span class="nb">round</span><span class="p">(</span><span class="n">eper</span> <span class="o">-</span> <span class="n">eiso</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;difference in (eV) is </span><span class="si">%f</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nb">round</span><span class="p">((</span><span class="n">eper</span> <span class="o">-</span> <span class="n">eiso</span><span class="p">)</span> <span class="o">*</span> <span class="n">hart_to_ev</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>

        <span class="n">es_corr</span> <span class="o">=</span> <span class="nb">round</span><span class="p">((</span><span class="n">eiso</span> <span class="o">-</span> <span class="n">eper</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">*</span> <span class="n">hart_to_ev</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Defect Correction without alignment </span><span class="si">%f</span><span class="s2"> (eV): &quot;</span><span class="p">,</span> <span class="n">es_corr</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">es_corr</span></div>

<div class="viewcode-block" id="FreysoldtCorrection.perform_pot_corr"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.FreysoldtCorrection.perform_pot_corr">[docs]</a>    <span class="k">def</span> <span class="nf">perform_pot_corr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                         <span class="n">axis_grid</span><span class="p">,</span>
                         <span class="n">pureavg</span><span class="p">,</span>
                         <span class="n">defavg</span><span class="p">,</span>
                         <span class="n">lattice</span><span class="p">,</span>
                         <span class="n">q</span><span class="p">,</span>
                         <span class="n">defect_frac_position</span><span class="p">,</span>
                         <span class="n">axis</span><span class="p">,</span>
                         <span class="n">widthsample</span><span class="o">=</span><span class="mf">1.0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        For performing planar averaging potential alignment</span>
<span class="sd">        Args:</span>
<span class="sd">             axis_grid (1 x NGX where NGX is the length of the NGX grid</span>
<span class="sd">                    in the axis direction. Same length as pureavg list):</span>
<span class="sd">                        A numpy array which contain the cartesian axis</span>
<span class="sd">                        values (in angstroms) that correspond to each planar avg</span>
<span class="sd">                        potential supplied.</span>
<span class="sd">             pureavg (1 x NGX where NGX is the length of the NGX grid in</span>
<span class="sd">                    the axis direction.):</span>
<span class="sd">                        A numpy array for the planar averaged</span>
<span class="sd">                        electrostatic potential of the bulk supercell.</span>
<span class="sd">             defavg (1 x NGX where NGX is the length of the NGX grid in</span>
<span class="sd">                    the axis direction.):</span>
<span class="sd">                        A numpy array for the planar averaged</span>
<span class="sd">                        electrostatic potential of the defect supercell.</span>
<span class="sd">             lattice: Pymatgen Lattice object of the defect supercell</span>
<span class="sd">             q (float or int): charge of the defect</span>
<span class="sd">             defect_frac_position: Fracitional Coordinates of the defect in the supercell</span>
<span class="sd">             axis (int): axis for performing the freysoldt correction on</span>
<span class="sd">             widthsample (float): width (in Angstroms) of the region in between defects</span>
<span class="sd">                where the potential alignment correction is averaged. Default is 1 Angstrom.</span>
<span class="sd">        Returns:</span>
<span class="sd">            Potential Alignment contribution to Freysoldt Correction (float)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;run Freysoldt potential alignment method for axis &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">axis</span><span class="p">))</span>
        <span class="n">nx</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">axis_grid</span><span class="p">)</span>

        <span class="c1"># shift these planar averages to have defect at origin</span>
        <span class="n">axfracval</span> <span class="o">=</span> <span class="n">defect_frac_position</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
        <span class="n">axbulkval</span> <span class="o">=</span> <span class="n">axfracval</span> <span class="o">*</span> <span class="n">lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">axbulkval</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">axbulkval</span> <span class="o">+=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">axbulkval</span> <span class="o">&gt;</span> <span class="n">lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">[</span><span class="n">axis</span><span class="p">]:</span>
            <span class="n">axbulkval</span> <span class="o">-=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">abc</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">axbulkval</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nx</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">axbulkval</span> <span class="o">&lt;</span> <span class="n">axis_grid</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
                    <span class="k">break</span>
            <span class="n">rollind</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">axis_grid</span><span class="p">)</span> <span class="o">-</span> <span class="n">i</span>
            <span class="n">pureavg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">roll</span><span class="p">(</span><span class="n">pureavg</span><span class="p">,</span> <span class="n">rollind</span><span class="p">)</span>
            <span class="n">defavg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">roll</span><span class="p">(</span><span class="n">defavg</span><span class="p">,</span> <span class="n">rollind</span><span class="p">)</span>

        <span class="c1"># if not self._silence:</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;calculating lr part along planar avg axis&quot;</span><span class="p">)</span>
        <span class="n">reci_latt</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">reciprocal_lattice</span>
        <span class="n">dg</span> <span class="o">=</span> <span class="n">reci_latt</span><span class="o">.</span><span class="n">abc</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
        <span class="n">dg</span> <span class="o">/=</span> <span class="n">ang_to_bohr</span>  <span class="c1"># convert to bohr to do calculation in atomic units</span>

        <span class="c1"># Build background charge potential with defect at origin</span>
        <span class="n">v_G</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">axis_grid</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s2">&quot;c16&quot;</span><span class="p">))</span>
        <span class="n">v_G</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="o">-</span><span class="n">q</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">q_model</span><span class="o">.</span><span class="n">rho_rec_limit0</span>
        <span class="n">g</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">roll</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="n">nx</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="n">nx</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">nx</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span> <span class="o">*</span> <span class="n">dg</span>
        <span class="n">g2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">g</span><span class="p">)[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="n">v_G</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">*</span> <span class="n">g2</span><span class="p">)</span> <span class="o">*</span> <span class="o">-</span><span class="n">q</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">q_model</span><span class="o">.</span><span class="n">rho_rec</span><span class="p">(</span><span class="n">g2</span><span class="p">)</span>
        <span class="n">v_G</span><span class="p">[</span><span class="n">nx</span> <span class="o">//</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">nx</span> <span class="o">%</span> <span class="mi">2</span><span class="p">)</span> <span class="k">else</span> <span class="n">v_G</span><span class="p">[</span><span class="n">nx</span> <span class="o">//</span> <span class="mi">2</span><span class="p">]</span>

        <span class="c1"># Get the real space potential by peforming a  fft and grabbing the imaginary portion</span>
        <span class="n">v_R</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fft</span><span class="p">(</span><span class="n">v_G</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">imag</span><span class="p">(</span><span class="n">v_R</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">())</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">madetol</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;imaginary part found to be </span><span class="si">%s</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">imag</span><span class="p">(</span><span class="n">v_R</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">()))</span>
        <span class="n">v_R</span> <span class="o">/=</span> <span class="p">(</span><span class="n">lattice</span><span class="o">.</span><span class="n">volume</span> <span class="o">*</span> <span class="n">ang_to_bohr</span> <span class="o">**</span> <span class="mi">3</span><span class="p">)</span>
        <span class="n">v_R</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">v_R</span><span class="p">)</span> <span class="o">*</span> <span class="n">hart_to_ev</span>

        <span class="c1"># get correction</span>
        <span class="n">short</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">defavg</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pureavg</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">v_R</span><span class="p">))</span>
        <span class="n">checkdis</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">widthsample</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">axis_grid</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">axis_grid</span><span class="p">[</span><span class="mi">0</span><span class="p">]))</span>
        <span class="n">mid</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">short</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>

        <span class="n">tmppot</span> <span class="o">=</span> <span class="p">[</span><span class="n">short</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">mid</span> <span class="o">-</span> <span class="n">checkdis</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="n">checkdis</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;shifted defect position on axis (</span><span class="si">%s</span><span class="s2">) to origin&quot;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">axbulkval</span><span class="p">))</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;means sampling region is (</span><span class="si">%f</span><span class="s2">,</span><span class="si">%f</span><span class="s2">)&quot;</span><span class="p">,</span> <span class="n">axis_grid</span><span class="p">[</span><span class="n">mid</span> <span class="o">-</span> <span class="n">checkdis</span><span class="p">],</span> <span class="n">axis_grid</span><span class="p">[</span><span class="n">mid</span> <span class="o">+</span> <span class="n">checkdis</span><span class="p">])</span>

        <span class="n">C</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">tmppot</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;C = </span><span class="si">%f</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">C</span><span class="p">)</span>
        <span class="n">final_shift</span> <span class="o">=</span> <span class="p">[</span><span class="n">short</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="n">C</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">v_R</span><span class="p">))]</span>
        <span class="n">v_R</span> <span class="o">=</span> <span class="p">[</span><span class="n">elmnt</span> <span class="o">-</span> <span class="n">C</span> <span class="k">for</span> <span class="n">elmnt</span> <span class="ow">in</span> <span class="n">v_R</span><span class="p">]</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;C value is averaged to be </span><span class="si">%f</span><span class="s2"> eV &quot;</span><span class="p">,</span> <span class="n">C</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Potentital alignment energy correction (-q*delta V):  </span><span class="si">%f</span><span class="s2"> (eV)&quot;</span><span class="p">,</span> <span class="o">-</span><span class="n">q</span> <span class="o">*</span> <span class="n">C</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pot_corr</span> <span class="o">=</span> <span class="o">-</span><span class="n">q</span> <span class="o">*</span> <span class="n">C</span>

        <span class="c1"># log plotting data:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;pot_plot_data&quot;</span><span class="p">][</span><span class="n">axis</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;Vr&quot;</span><span class="p">:</span> <span class="n">v_R</span><span class="p">,</span>
            <span class="s2">&quot;x&quot;</span><span class="p">:</span> <span class="n">axis_grid</span><span class="p">,</span>
            <span class="s2">&quot;dft_diff&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">defavg</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">pureavg</span><span class="p">),</span>
            <span class="s2">&quot;final_shift&quot;</span><span class="p">:</span> <span class="n">final_shift</span><span class="p">,</span>
            <span class="s2">&quot;check&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">mid</span> <span class="o">-</span> <span class="n">checkdis</span><span class="p">,</span> <span class="n">mid</span> <span class="o">+</span> <span class="n">checkdis</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
        <span class="p">}</span>

        <span class="c1"># log uncertainty:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;pot_corr_uncertainty_md&quot;</span><span class="p">][</span><span class="n">axis</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;stats&quot;</span><span class="p">:</span> <span class="n">stats</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">tmppot</span><span class="p">)</span><span class="o">.</span><span class="n">_asdict</span><span class="p">(),</span> <span class="s2">&quot;potcorr&quot;</span><span class="p">:</span> <span class="o">-</span><span class="n">q</span> <span class="o">*</span> <span class="n">C</span><span class="p">}</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pot_corr</span></div>

<div class="viewcode-block" id="FreysoldtCorrection.plot"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.FreysoldtCorrection.plot">[docs]</a>    <span class="k">def</span> <span class="nf">plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">axis</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">saved</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plots the planar average electrostatic potential against the Long range and</span>
<span class="sd">        short range models from Freysoldt. Must run perform_pot_corr or get_correction</span>
<span class="sd">        (to load metadata) before this can be used.</span>
<span class="sd">        Args:</span>
<span class="sd">             axis (int): axis to plot</span>
<span class="sd">             title (str): Title to be given to plot. Default is no title.</span>
<span class="sd">             saved (bool): Whether to save file or not. If False then returns plot</span>
<span class="sd">                object. If True then saves plot as   str(title) + &quot;FreyplnravgPlot.pdf&quot;</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;pot_plot_data&quot;</span><span class="p">]:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot plot potential alignment before running correction!&quot;</span><span class="p">)</span>

        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;pot_plot_data&#39;</span><span class="p">][</span><span class="n">axis</span><span class="p">][</span><span class="s1">&#39;x&#39;</span><span class="p">]</span>
        <span class="n">v_R</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;pot_plot_data&#39;</span><span class="p">][</span><span class="n">axis</span><span class="p">][</span><span class="s1">&#39;Vr&#39;</span><span class="p">]</span>
        <span class="n">dft_diff</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;pot_plot_data&#39;</span><span class="p">][</span><span class="n">axis</span><span class="p">][</span><span class="s1">&#39;dft_diff&#39;</span><span class="p">]</span>
        <span class="n">final_shift</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;pot_plot_data&#39;</span><span class="p">][</span><span class="n">axis</span><span class="p">][</span><span class="s1">&#39;final_shift&#39;</span><span class="p">]</span>
        <span class="n">check</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;pot_plot_data&#39;</span><span class="p">][</span><span class="n">axis</span><span class="p">][</span><span class="s1">&#39;check&#39;</span><span class="p">]</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">v_R</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s2">&quot;green&quot;</span><span class="p">,</span> <span class="n">zorder</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">&quot;long range from model&quot;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">dft_diff</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">&quot;DFT locpot diff&quot;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">final_shift</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="s2">&quot;blue&quot;</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">&quot;short range (aligned)&quot;</span><span class="p">)</span>

        <span class="n">tmpx</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">check</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">check</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">fill_between</span><span class="p">(</span><span class="n">tmpx</span><span class="p">,</span> <span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.15</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s2">&quot;sampling region&quot;</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="nb">round</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>
        <span class="n">ymin</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">v_R</span><span class="p">),</span> <span class="nb">min</span><span class="p">(</span><span class="n">dft_diff</span><span class="p">),</span> <span class="nb">min</span><span class="p">(</span><span class="n">final_shift</span><span class="p">))</span>
        <span class="n">ymax</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">v_R</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">dft_diff</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">final_shift</span><span class="p">))</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mf">0.2</span> <span class="o">+</span> <span class="n">ymin</span><span class="p">,</span> <span class="mf">0.2</span> <span class="o">+</span> <span class="n">ymax</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;distance along axis ($\AA$)&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s2">&quot;Potential (V)&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">9</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s2">&quot;black&quot;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; defect potential&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">18</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">saved</span><span class="p">:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;FreyplnravgPlot.pdf&quot;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">plt</span></div></div>


<div class="viewcode-block" id="KumagaiCorrection"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection">[docs]</a><span class="k">class</span> <span class="nc">KumagaiCorrection</span><span class="p">(</span><span class="n">DefectCorrection</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class for KumagaiCorrection class. Largely adapated from PyCDT code</span>

<span class="sd">    If this correction is used, please reference Kumagai and Oba&#39;s original paper</span>
<span class="sd">    (doi: 10.1103/PhysRevB.89.195205) as well as Freysoldt&#39;s original</span>
<span class="sd">    paper (doi: 10.1103/PhysRevLett.102.016402)</span>

<span class="sd">    NOTE that equations 8 and 9 from Kumagai et al. reference are divided by (4 pi) to get SI units</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">dielectric_tensor</span><span class="p">,</span>
                 <span class="n">sampling_radius</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">gamma</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes the Kumagai Correction</span>
<span class="sd">        Args:</span>
<span class="sd">            dielectric_tensor (float or 3x3 matrix): Dielectric constant for the structure</span>

<span class="sd">            optional data that can be tuned:</span>
<span class="sd">                sampling_radius (float): radius (in Angstrom) which sites must be outside</span>
<span class="sd">                    of to be included in the correction. Publication by Kumagai advises to</span>
<span class="sd">                    use Wigner-Seitz radius of defect supercell, so this is default value.</span>
<span class="sd">                gamma (float): convergence parameter for gamma function.</span>
<span class="sd">                    Code will automatically determine this if set to None.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;gamma&quot;</span><span class="p">:</span> <span class="n">gamma</span><span class="p">,</span> <span class="s2">&quot;sampling_radius&quot;</span><span class="p">:</span> <span class="n">sampling_radius</span><span class="p">,</span> <span class="s2">&quot;potalign&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">}</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">dielectric_tensor</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="ow">or</span> \
                <span class="nb">isinstance</span><span class="p">(</span><span class="n">dielectric_tensor</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">identity</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="o">*</span> <span class="n">dielectric_tensor</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">dielectric_tensor</span><span class="p">)</span>

<div class="viewcode-block" id="KumagaiCorrection.get_correction"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.get_correction">[docs]</a>    <span class="k">def</span> <span class="nf">get_correction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the Kumagai correction for a defect entry</span>
<span class="sd">        Args:</span>
<span class="sd">            entry (DefectEntry): defect entry to compute Kumagai correction on.</span>

<span class="sd">                Requires following parameters in the DefectEntry to exist:</span>

<span class="sd">                    bulk_atomic_site_averages (list):  list of bulk structure&quot;s atomic site averaged ESPs * charge,</span>
<span class="sd">                        in same order as indices of bulk structure</span>
<span class="sd">                        note this is list given by VASP&#39;s OUTCAR (so it is multiplied by a test charge of -1)</span>

<span class="sd">                    defect_atomic_site_averages (list):  list of defect structure&quot;s atomic site averaged ESPs * charge,</span>
<span class="sd">                        in same order as indices of defect structure</span>
<span class="sd">                        note this is list given by VASP&#39;s OUTCAR (so it is multiplied by a test charge of -1)</span>

<span class="sd">                    site_matching_indices (list):  list of corresponding site index values for</span>
<span class="sd">                        bulk and defect site structures EXCLUDING the defect site itself</span>
<span class="sd">                        (ex. [[bulk structure site index, defect structure&quot;s corresponding site index], ... ]</span>

<span class="sd">                    initial_defect_structure (Structure): Pymatgen Structure object representing un-relaxed defect</span>
<span class="sd">                        structure</span>

<span class="sd">                    defect_frac_sc_coords (array): Defect Position in fractional coordinates of the supercell</span>
<span class="sd">                        given in bulk_structure</span>
<span class="sd">        Returns:</span>
<span class="sd">            KumagaiCorrection values as a dictionary</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bulk_atomic_site_averages</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;bulk_atomic_site_averages&quot;</span><span class="p">]</span>
        <span class="n">defect_atomic_site_averages</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;defect_atomic_site_averages&quot;</span><span class="p">]</span>
        <span class="n">site_matching_indices</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;site_matching_indices&quot;</span><span class="p">]</span>
        <span class="n">defect_sc_structure</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;initial_defect_structure&quot;</span><span class="p">]</span>
        <span class="n">defect_frac_sc_coords</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;defect_frac_sc_coords&quot;</span><span class="p">]</span>

        <span class="n">lattice</span> <span class="o">=</span> <span class="n">defect_sc_structure</span><span class="o">.</span><span class="n">lattice</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">q</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">defect</span><span class="o">.</span><span class="n">charge</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">tune_for_gamma</span><span class="p">(</span><span class="n">lattice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">)</span>

        <span class="n">prec_set</span> <span class="o">=</span> <span class="p">[</span><span class="mi">25</span><span class="p">,</span> <span class="mi">28</span><span class="p">]</span>
        <span class="n">g_vecs</span><span class="p">,</span> <span class="n">recip_summation</span><span class="p">,</span> <span class="n">r_vecs</span><span class="p">,</span> <span class="n">real_summation</span> <span class="o">=</span> <span class="n">generate_R_and_G_vecs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">],</span>
                                                                                <span class="n">prec_set</span><span class="p">,</span> <span class="n">lattice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">)</span>

        <span class="n">pot_shift</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_potential_shift</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">],</span> <span class="n">volume</span><span class="p">)</span>
        <span class="n">si</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_self_interaction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">])</span>
        <span class="n">es_corr</span> <span class="o">=</span> <span class="p">[(</span><span class="n">real_summation</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span> <span class="o">+</span> <span class="n">recip_summation</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span> <span class="o">+</span> <span class="n">pot_shift</span> <span class="o">+</span> <span class="n">si</span><span class="p">)</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)]</span>

        <span class="c1"># increase precision if correction is not converged yet</span>
        <span class="c1"># TODO: allow for larger prec_set to be tried if this fails</span>
        <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">es_corr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">es_corr</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mf">0.0001</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Es_corr summation not converged! (</span><span class="si">{}</span><span class="s2"> vs. </span><span class="si">{}</span><span class="s2">)</span><span class="se">\n</span><span class="s2">Trying a larger prec_set...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">es_corr</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                                                                                            <span class="n">es_corr</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
            <span class="n">prec_set</span> <span class="o">=</span> <span class="p">[</span><span class="mi">30</span><span class="p">,</span> <span class="mi">35</span><span class="p">]</span>
            <span class="n">g_vecs</span><span class="p">,</span> <span class="n">recip_summation</span><span class="p">,</span> <span class="n">r_vecs</span><span class="p">,</span> <span class="n">real_summation</span> <span class="o">=</span> <span class="n">generate_R_and_G_vecs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">],</span>
                                                                                    <span class="n">prec_set</span><span class="p">,</span> <span class="n">lattice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">)</span>
            <span class="n">es_corr</span> <span class="o">=</span> <span class="p">[(</span><span class="n">real_summation</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span> <span class="o">+</span> <span class="n">recip_summation</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span> <span class="o">+</span> <span class="n">pot_shift</span> <span class="o">+</span> <span class="n">si</span><span class="p">)</span> <span class="k">for</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)]</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">es_corr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">es_corr</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;</span> <span class="mf">0.0001</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Correction still not converged after trying prec_sets up to 35... serious error.&quot;</span><span class="p">)</span>

        <span class="n">es_corr</span> <span class="o">=</span> <span class="n">es_corr</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="o">-</span><span class="p">(</span><span class="n">q</span> <span class="o">**</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">*</span> <span class="n">kumagai_to_V</span> <span class="o">/</span> <span class="mf">2.</span>  <span class="c1"># [eV]</span>

        <span class="c1"># if no sampling radius specified for pot align, then assuming Wigner-Seitz radius:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;sampling_radius&quot;</span><span class="p">]:</span>
            <span class="n">wz</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">get_wigner_seitz_cell</span><span class="p">()</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">facet</span> <span class="ow">in</span> <span class="n">wz</span><span class="p">:</span>
                <span class="n">midpt</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">facet</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
                <span class="n">dist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">midpt</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;sampling_radius&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>

        <span class="c1"># assemble site_list based on matching indices</span>
        <span class="c1"># [[defect_site object, Vqb for site], .. repeat for all non defective sites]</span>
        <span class="n">site_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">bs_ind</span><span class="p">,</span> <span class="n">ds_ind</span> <span class="ow">in</span> <span class="n">site_matching_indices</span><span class="p">:</span>
            <span class="n">Vqb</span> <span class="o">=</span> <span class="o">-</span><span class="p">(</span><span class="n">defect_atomic_site_averages</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">ds_ind</span><span class="p">)]</span> <span class="o">-</span> <span class="n">bulk_atomic_site_averages</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">bs_ind</span><span class="p">)])</span>
            <span class="n">site_list</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">defect_sc_structure</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">ds_ind</span><span class="p">)],</span> <span class="n">Vqb</span><span class="p">])</span>

        <span class="n">pot_corr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">perform_pot_corr</span><span class="p">(</span><span class="n">defect_sc_structure</span><span class="p">,</span> <span class="n">defect_frac_sc_coords</span><span class="p">,</span> <span class="n">site_list</span><span class="p">,</span>
                                         <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;sampling_radius&quot;</span><span class="p">],</span> <span class="n">q</span><span class="p">,</span> <span class="n">r_vecs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                         <span class="n">g_vecs</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;gamma&quot;</span><span class="p">])</span>

        <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;kumagai_meta&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">)</span>
        <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;potalign&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pot_corr</span> <span class="o">/</span> <span class="p">(</span><span class="o">-</span><span class="n">q</span><span class="p">)</span> <span class="k">if</span> <span class="n">q</span> <span class="k">else</span> <span class="mf">0.</span>

        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;kumagai_electrostatic&quot;</span><span class="p">:</span> <span class="n">es_corr</span><span class="p">,</span> <span class="s2">&quot;kumagai_potential_alignment&quot;</span><span class="p">:</span> <span class="n">pot_corr</span><span class="p">}</span></div>

<div class="viewcode-block" id="KumagaiCorrection.perform_es_corr"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.perform_es_corr">[docs]</a>    <span class="k">def</span> <span class="nf">perform_es_corr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">prec</span><span class="p">,</span> <span class="n">lattice</span><span class="p">,</span> <span class="n">charge</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Peform Electrostatic Kumagai Correction</span>
<span class="sd">        Args:</span>
<span class="sd">            gamma (float): Ewald parameter</span>
<span class="sd">            prec (int): Precision parameter for reciprical/real lattice vector generation</span>
<span class="sd">            lattice: Pymatgen Lattice object corresponding to defect supercell</span>
<span class="sd">            charge (int): Defect charge</span>
<span class="sd">        Return:</span>
<span class="sd">            Electrostatic Point Charge contribution to Kumagai Correction (float)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">lattice</span><span class="o">.</span><span class="n">volume</span>

        <span class="n">g_vecs</span><span class="p">,</span> <span class="n">recip_summation</span><span class="p">,</span> <span class="n">r_vecs</span><span class="p">,</span> <span class="n">real_summation</span> <span class="o">=</span> <span class="n">generate_R_and_G_vecs</span><span class="p">(</span><span class="n">gamma</span><span class="p">,</span> <span class="p">[</span><span class="n">prec</span><span class="p">],</span> <span class="n">lattice</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">)</span>
        <span class="n">recip_summation</span> <span class="o">=</span> <span class="n">recip_summation</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">real_summation</span> <span class="o">=</span> <span class="n">real_summation</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="n">es_corr</span> <span class="o">=</span> <span class="p">(</span><span class="n">recip_summation</span> <span class="o">+</span> <span class="n">real_summation</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_potential_shift</span><span class="p">(</span><span class="n">gamma</span><span class="p">,</span> <span class="n">volume</span><span class="p">)</span> <span class="o">+</span>
                   <span class="bp">self</span><span class="o">.</span><span class="n">get_self_interaction</span><span class="p">(</span><span class="n">gamma</span><span class="p">))</span>

        <span class="n">es_corr</span> <span class="o">*=</span> <span class="o">-</span><span class="p">(</span><span class="n">charge</span> <span class="o">**</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">*</span> <span class="n">kumagai_to_V</span> <span class="o">/</span> <span class="mf">2.</span>  <span class="c1"># [eV]</span>

        <span class="k">return</span> <span class="n">es_corr</span></div>

<div class="viewcode-block" id="KumagaiCorrection.perform_pot_corr"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.perform_pot_corr">[docs]</a>    <span class="k">def</span> <span class="nf">perform_pot_corr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">defect_structure</span><span class="p">,</span> <span class="n">defect_frac_coords</span><span class="p">,</span> <span class="n">site_list</span><span class="p">,</span>
                         <span class="n">sampling_radius</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="n">r_vecs</span><span class="p">,</span> <span class="n">g_vecs</span><span class="p">,</span> <span class="n">gamma</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        For performing potential alignment in manner described by Kumagai et al.</span>
<span class="sd">        Args:</span>
<span class="sd">            defect_structure: Pymatgen Structure object corrsponding to the defect supercell</span>

<span class="sd">            defect_frac_coords (array): Defect Position in fractional coordinates of the supercell</span>
<span class="sd">                given in bulk_structure</span>

<span class="sd">            site_list: list of corresponding site index values for</span>
<span class="sd">                bulk and defect site structures EXCLUDING the defect site itself</span>
<span class="sd">                (ex. [[bulk structure site index, defect structure&quot;s corresponding site index], ... ]</span>

<span class="sd">            sampling_radius (float): radius (in Angstrom) which sites must be outside</span>
<span class="sd">                of to be included in the correction. Publication by Kumagai advises to</span>
<span class="sd">                use Wigner-Seitz radius of defect supercell, so this is default value.</span>

<span class="sd">            q (int): Defect charge</span>

<span class="sd">            r_vecs: List of real lattice vectors to use in summation</span>

<span class="sd">            g_vecs: List of reciprocal lattice vectors to use in summation</span>

<span class="sd">            gamma (float): Ewald parameter</span>

<span class="sd">        Return:</span>
<span class="sd">            Potential alignment contribution to Kumagai Correction (float)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">volume</span> <span class="o">=</span> <span class="n">defect_structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">volume</span>
        <span class="n">potential_shift</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_potential_shift</span><span class="p">(</span><span class="n">gamma</span><span class="p">,</span> <span class="n">volume</span><span class="p">)</span>

        <span class="n">pot_dict</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># keys will be site index in the defect structure</span>
        <span class="n">for_correction</span> <span class="o">=</span> <span class="p">[]</span>  <span class="c1"># region to sample for correction</span>

        <span class="c1"># for each atom, do the following:</span>
        <span class="c1"># (a) get relative_vector from defect_site to site in defect_supercell structure</span>
        <span class="c1"># (b) recalculate the recip and real summation values based on this r_vec</span>
        <span class="c1"># (c) get information needed for pot align</span>
        <span class="k">for</span> <span class="n">site</span><span class="p">,</span> <span class="n">Vqb</span> <span class="ow">in</span> <span class="n">site_list</span><span class="p">:</span>
            <span class="n">dist</span><span class="p">,</span> <span class="n">jimage</span> <span class="o">=</span> <span class="n">site</span><span class="o">.</span><span class="n">distance_and_image_from_frac_coords</span><span class="p">(</span><span class="n">defect_frac_coords</span><span class="p">)</span>
            <span class="n">vec_defect_to_site</span> <span class="o">=</span> <span class="n">defect_structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">get_cartesian_coords</span><span class="p">(</span><span class="n">site</span><span class="o">.</span><span class="n">frac_coords</span> <span class="o">-</span>
                                                                               <span class="n">jimage</span> <span class="o">-</span> <span class="n">defect_frac_coords</span><span class="p">)</span>
            <span class="n">dist_to_defect</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">vec_defect_to_site</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">dist_to_defect</span> <span class="o">-</span> <span class="n">dist</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">0.001</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Error in computing vector to defect&quot;</span><span class="p">)</span>

            <span class="n">relative_real_vectors</span> <span class="o">=</span> <span class="p">[</span><span class="n">r_vec</span> <span class="o">-</span> <span class="n">vec_defect_to_site</span> <span class="k">for</span> <span class="n">r_vec</span> <span class="ow">in</span> <span class="n">r_vecs</span><span class="p">[:]]</span>

            <span class="n">real_sum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_real_summation</span><span class="p">(</span><span class="n">gamma</span><span class="p">,</span> <span class="n">relative_real_vectors</span><span class="p">)</span>
            <span class="n">recip_sum</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_recip_summation</span><span class="p">(</span><span class="n">gamma</span><span class="p">,</span> <span class="n">g_vecs</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="n">vec_defect_to_site</span><span class="p">[:])</span>

            <span class="n">Vpc</span> <span class="o">=</span> <span class="p">(</span><span class="n">real_sum</span> <span class="o">+</span> <span class="n">recip_sum</span> <span class="o">+</span> <span class="n">potential_shift</span><span class="p">)</span> <span class="o">*</span> <span class="n">kumagai_to_V</span> <span class="o">*</span> <span class="n">q</span>

            <span class="n">defect_struct_index</span> <span class="o">=</span> <span class="n">defect_structure</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
            <span class="n">pot_dict</span><span class="p">[</span><span class="n">defect_struct_index</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s2">&quot;Vpc&quot;</span><span class="p">:</span> <span class="n">Vpc</span><span class="p">,</span>
                <span class="s2">&quot;Vqb&quot;</span><span class="p">:</span> <span class="n">Vqb</span><span class="p">,</span>
                <span class="s2">&quot;dist_to_defect&quot;</span><span class="p">:</span> <span class="n">dist_to_defect</span>
            <span class="p">}</span>

            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;For atom </span><span class="si">{}</span><span class="se">\n\t</span><span class="s2">bulk/defect DFT potential difference = &quot;</span>
                         <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">defect_struct_index</span><span class="p">,</span> <span class="n">Vqb</span><span class="p">))</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">anisotropic model charge: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">Vpc</span><span class="p">))</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t\t</span><span class="s2">reciprocal part: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">recip_sum</span> <span class="o">*</span> <span class="n">kumagai_to_V</span> <span class="o">*</span> <span class="n">q</span><span class="p">))</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t\t</span><span class="s2">real part: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">real_sum</span> <span class="o">*</span> <span class="n">kumagai_to_V</span> <span class="o">*</span> <span class="n">q</span><span class="p">))</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t\t</span><span class="s2">self interaction part: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">potential_shift</span> <span class="o">*</span> <span class="n">kumagai_to_V</span> <span class="o">*</span> <span class="n">q</span><span class="p">))</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">relative_vector to defect: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">vec_defect_to_site</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">dist_to_defect</span> <span class="o">&gt;</span> <span class="n">sampling_radius</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">distance to defect is </span><span class="si">{}</span><span class="s2"> which is outside minimum sampling &quot;</span>
                             <span class="s2">&quot;radius </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dist_to_defect</span><span class="p">,</span>
                                                <span class="n">sampling_radius</span><span class="p">))</span>
                <span class="n">for_correction</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Vqb</span> <span class="o">-</span> <span class="n">Vpc</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2">distance to defect is </span><span class="si">{}</span><span class="s2"> which is inside minimum sampling &quot;</span>
                             <span class="s2">&quot;radius </span><span class="si">{}</span><span class="s2"> (so will not include for correction)&quot;</span>
                             <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">dist_to_defect</span><span class="p">,</span> <span class="n">sampling_radius</span><span class="p">))</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">for_correction</span><span class="p">):</span>
            <span class="n">pot_alignment</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">for_correction</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;No atoms sampled for_correction radius!&quot;</span>
                        <span class="s2">&quot; Assigning potential alignment value of 0.&quot;</span><span class="p">)</span>
            <span class="n">pot_alignment</span> <span class="o">=</span> <span class="mf">0.</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;potalign&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pot_alignment</span>
        <span class="n">pot_corr</span> <span class="o">=</span> <span class="o">-</span><span class="n">q</span> <span class="o">*</span> <span class="n">pot_alignment</span>

        <span class="c1"># log uncertainty stats:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;pot_corr_uncertainty_md&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;stats&quot;</span><span class="p">:</span> <span class="n">stats</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">for_correction</span><span class="p">)</span><span class="o">.</span><span class="n">_asdict</span><span class="p">(),</span>
            <span class="s2">&quot;number_sampled&quot;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">for_correction</span><span class="p">)</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;pot_plot_data&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pot_dict</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Kumagai potential alignment (site averaging): </span><span class="si">%f</span><span class="s2">&quot;</span><span class="p">,</span> <span class="n">pot_alignment</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Kumagai potential alignment correction energy: </span><span class="si">%f</span><span class="s2"> eV&quot;</span><span class="p">,</span> <span class="n">pot_corr</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">pot_corr</span></div>

<div class="viewcode-block" id="KumagaiCorrection.get_real_summation"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.get_real_summation">[docs]</a>    <span class="k">def</span> <span class="nf">get_real_summation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">real_vectors</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get real summation term from list of real-space vectors</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">real_part</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">invepsilon</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">)</span>
        <span class="n">rd_epsilon</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">r_vec</span> <span class="ow">in</span> <span class="n">real_vectors</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">r_vec</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">1e-8</span><span class="p">:</span>
                <span class="n">loc_res</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">r_vec</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">invepsilon</span><span class="p">,</span> <span class="n">r_vec</span><span class="p">)))</span>
                <span class="n">nmr</span> <span class="o">=</span> <span class="n">scipy</span><span class="o">.</span><span class="n">special</span><span class="o">.</span><span class="n">erfc</span><span class="p">(</span><span class="n">gamma</span> <span class="o">*</span> <span class="n">loc_res</span><span class="p">)</span>
                <span class="n">real_part</span> <span class="o">+=</span> <span class="n">nmr</span> <span class="o">/</span> <span class="n">loc_res</span>

        <span class="n">real_part</span> <span class="o">/=</span> <span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">rd_epsilon</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">real_part</span></div>

<div class="viewcode-block" id="KumagaiCorrection.get_recip_summation"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.get_recip_summation">[docs]</a>    <span class="k">def</span> <span class="nf">get_recip_summation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">recip_vectors</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="p">[</span><span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">]):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get Reciprocal summation term from list of reciprocal-space vectors</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">recip_part</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="n">g_vec</span> <span class="ow">in</span> <span class="n">recip_vectors</span><span class="p">:</span>
            <span class="c1"># dont need to avoid G=0, because it will not be</span>
            <span class="c1"># in recip list (if generate_R_and_G_vecs is used)</span>
            <span class="n">Gdotdiel</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">g_vec</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">,</span> <span class="n">g_vec</span><span class="p">))</span>
            <span class="n">summand</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">Gdotdiel</span> <span class="o">/</span> <span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="p">(</span><span class="n">gamma</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)))</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">g_vec</span><span class="p">,</span> <span class="n">r</span><span class="p">))</span> <span class="o">/</span> <span class="n">Gdotdiel</span>
            <span class="n">recip_part</span> <span class="o">+=</span> <span class="n">summand</span>

        <span class="n">recip_part</span> <span class="o">/=</span> <span class="n">volume</span>

        <span class="k">return</span> <span class="n">recip_part</span></div>

<div class="viewcode-block" id="KumagaiCorrection.get_self_interaction"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.get_self_interaction">[docs]</a>    <span class="k">def</span> <span class="nf">get_self_interaction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gamma</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            gamma ():</span>

<span class="sd">        Returns:</span>
<span class="sd">            Self-interaction energy of defect.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">determ</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">det</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dielectric</span><span class="p">)</span>
        <span class="k">return</span> <span class="o">-</span> <span class="n">gamma</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="n">determ</span><span class="p">))</span></div>

<div class="viewcode-block" id="KumagaiCorrection.get_potential_shift"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.get_potential_shift">[docs]</a>    <span class="k">def</span> <span class="nf">get_potential_shift</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">volume</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            gamma (float): Gamma</span>
<span class="sd">            volume (float): Volume.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Potential shift for defect.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="o">-</span> <span class="mf">0.25</span> <span class="o">/</span> <span class="p">(</span><span class="n">volume</span> <span class="o">*</span> <span class="n">gamma</span> <span class="o">**</span> <span class="mf">2.</span><span class="p">)</span></div>

<div class="viewcode-block" id="KumagaiCorrection.plot"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.KumagaiCorrection.plot">[docs]</a>    <span class="k">def</span> <span class="nf">plot</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">saved</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Plots the AtomicSite electrostatic potential against the Long range and short range models</span>
<span class="sd">        from Kumagai and Oba (doi: 10.1103/PhysRevB.89.195205)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="s2">&quot;pot_plot_data&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot plot potential alignment before running correction!&quot;</span><span class="p">)</span>

        <span class="n">sampling_radius</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;sampling_radius&quot;</span><span class="p">]</span>
        <span class="n">site_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;pot_plot_data&quot;</span><span class="p">]</span>
        <span class="n">potalign</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;potalign&quot;</span><span class="p">]</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">clf</span><span class="p">()</span>

        <span class="n">distances</span><span class="p">,</span> <span class="n">sample_region</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
        <span class="n">Vqb_list</span><span class="p">,</span> <span class="n">Vpc_list</span><span class="p">,</span> <span class="n">diff_list</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[],</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">site_ind</span><span class="p">,</span> <span class="n">site_dict</span> <span class="ow">in</span> <span class="n">site_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">dist</span> <span class="o">=</span> <span class="n">site_dict</span><span class="p">[</span><span class="s2">&quot;dist_to_defect&quot;</span><span class="p">]</span>
            <span class="n">distances</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">dist</span><span class="p">)</span>

            <span class="n">Vqb</span> <span class="o">=</span> <span class="n">site_dict</span><span class="p">[</span><span class="s2">&quot;Vqb&quot;</span><span class="p">]</span>
            <span class="n">Vpc</span> <span class="o">=</span> <span class="n">site_dict</span><span class="p">[</span><span class="s2">&quot;Vpc&quot;</span><span class="p">]</span>

            <span class="n">Vqb_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Vqb</span><span class="p">)</span>
            <span class="n">Vpc_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Vpc</span><span class="p">)</span>
            <span class="n">diff_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Vqb</span> <span class="o">-</span> <span class="n">Vpc</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">dist</span> <span class="o">&gt;</span> <span class="n">sampling_radius</span><span class="p">:</span>
                <span class="n">sample_region</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Vqb</span> <span class="o">-</span> <span class="n">Vpc</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">distances</span><span class="p">,</span> <span class="n">Vqb_list</span><span class="p">,</span>
                 <span class="n">color</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">&#39;^&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;None&#39;</span><span class="p">,</span>
                 <span class="n">label</span><span class="o">=</span><span class="s1">&#39;$V_{q/b}$&#39;</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">distances</span><span class="p">,</span> <span class="n">Vpc_list</span><span class="p">,</span>
                 <span class="n">color</span><span class="o">=</span><span class="s1">&#39;g&#39;</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">&#39;o&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;None&#39;</span><span class="p">,</span>
                 <span class="n">label</span><span class="o">=</span><span class="s1">&#39;$V_</span><span class="si">{pc}</span><span class="s1">$&#39;</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">distances</span><span class="p">,</span> <span class="n">diff_list</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="n">marker</span><span class="o">=</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">linestyle</span><span class="o">=</span><span class="s1">&#39;None&#39;</span><span class="p">,</span>
                 <span class="n">label</span><span class="o">=</span><span class="s1">&#39;$V_{q/b}$ - $V_</span><span class="si">{pc}</span><span class="s1">$&#39;</span><span class="p">)</span>

        <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">sampling_radius</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">distances</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.05</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
        <span class="n">y_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">Vqb_list</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">Vpc_list</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">diff_list</span><span class="p">))</span> <span class="o">+</span> <span class="o">.</span><span class="mi">1</span>
        <span class="n">y_min</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">Vqb_list</span><span class="p">),</span> <span class="nb">min</span><span class="p">(</span><span class="n">Vpc_list</span><span class="p">),</span> <span class="nb">min</span><span class="p">(</span><span class="n">diff_list</span><span class="p">))</span> <span class="o">-</span> <span class="o">.</span><span class="mi">1</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">fill_between</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y_min</span><span class="p">,</span> <span class="n">y_max</span><span class="p">,</span> <span class="n">facecolor</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span>
                         <span class="n">alpha</span><span class="o">=</span><span class="mf">0.15</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;sampling region&#39;</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">potalign</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;red&#39;</span><span class="p">,</span>
                    <span class="n">label</span><span class="o">=</span><span class="s1">&#39;pot. align. / -q&#39;</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">legend</span><span class="p">(</span><span class="n">loc</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">axhline</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="s1">&#39;black&#39;</span><span class="p">)</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">([</span><span class="n">y_min</span><span class="p">,</span> <span class="n">y_max</span><span class="p">])</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="n">distances</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.1</span><span class="p">])</span>

        <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;Distance from defect ($\AA$)&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;Potential (V)&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
        <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; atomic site potential plot&quot;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">saved</span><span class="p">:</span>
            <span class="n">plt</span><span class="o">.</span><span class="n">savefig</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;KumagaiESPavgPlot.pdf&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">plt</span></div></div>


<div class="viewcode-block" id="BandFillingCorrection"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.BandFillingCorrection">[docs]</a><span class="k">class</span> <span class="nc">BandFillingCorrection</span><span class="p">(</span><span class="n">DefectCorrection</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class for BandFillingCorrection class. Largely adapted from PyCDT code</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">resolution</span><span class="o">=</span><span class="mf">0.01</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes the Bandfilling correction</span>

<span class="sd">        Args:</span>
<span class="sd">            resolution (float): energy resolution to maintain for gap states</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resolution</span> <span class="o">=</span> <span class="n">resolution</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;num_hole_vbm&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s2">&quot;num_elec_cbm&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
            <span class="s2">&quot;potalign&quot;</span><span class="p">:</span> <span class="kc">None</span>
        <span class="p">}</span>

<div class="viewcode-block" id="BandFillingCorrection.get_correction"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.BandFillingCorrection.get_correction">[docs]</a>    <span class="k">def</span> <span class="nf">get_correction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the BandFilling correction for a defect entry</span>
<span class="sd">        Args:</span>
<span class="sd">            entry (DefectEntry): defect entry to compute BandFilling correction on.</span>
<span class="sd">                Requires following parameters in the DefectEntry to exist:</span>
<span class="sd">                    eigenvalues</span>
<span class="sd">                        dictionary of defect eigenvalues, as stored in a Vasprun object</span>

<span class="sd">                    kpoint_weights (list of floats)</span>
<span class="sd">                        kpoint weights corresponding to the dictionary of eigenvalues,</span>
<span class="sd">                        as stored in a Vasprun object</span>

<span class="sd">                    potalign (float)</span>
<span class="sd">                        potential alignment for the defect calculation</span>
<span class="sd">                        Only applies to non-zero charge,</span>
<span class="sd">                        When using potential alignment correction (freysoldt or kumagai),</span>
<span class="sd">                        need to divide by -q</span>

<span class="sd">                    cbm (float)</span>
<span class="sd">                        CBM of bulk calculation (or band structure calculation of bulk);</span>
<span class="sd">                        calculated on same level of theory as the defect</span>
<span class="sd">                        (ex. GGA defects -&gt; requires GGA cbm)</span>

<span class="sd">                    vbm (float)</span>
<span class="sd">                        VBM of bulk calculation (or band structure calculation of bulk);</span>
<span class="sd">                        calculated on same level of theory as the defect</span>
<span class="sd">                        (ex. GGA defects -&gt; requires GGA vbm)</span>
<span class="sd">        Returns:</span>
<span class="sd">            Bandfilling Correction value as a dictionary</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">eigenvalues</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;eigenvalues&quot;</span><span class="p">]</span>
        <span class="n">kpoint_weights</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;kpoint_weights&quot;</span><span class="p">]</span>
        <span class="n">potalign</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;potalign&quot;</span><span class="p">]</span>
        <span class="n">vbm</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;vbm&quot;</span><span class="p">]</span>
        <span class="n">cbm</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;cbm&quot;</span><span class="p">]</span>

        <span class="n">bf_corr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">perform_bandfill_corr</span><span class="p">(</span><span class="n">eigenvalues</span><span class="p">,</span> <span class="n">kpoint_weights</span><span class="p">,</span> <span class="n">potalign</span><span class="p">,</span> <span class="n">vbm</span><span class="p">,</span> <span class="n">cbm</span><span class="p">)</span>

        <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;bandfilling_meta&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;bandfilling_correction&quot;</span><span class="p">:</span> <span class="n">bf_corr</span><span class="p">}</span></div>

<div class="viewcode-block" id="BandFillingCorrection.perform_bandfill_corr"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.BandFillingCorrection.perform_bandfill_corr">[docs]</a>    <span class="k">def</span> <span class="nf">perform_bandfill_corr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">eigenvalues</span><span class="p">,</span> <span class="n">kpoint_weights</span><span class="p">,</span> <span class="n">potalign</span><span class="p">,</span> <span class="n">vbm</span><span class="p">,</span> <span class="n">cbm</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This calculates the band filling correction based on excess of electrons/holes in CB/VB...</span>

<span class="sd">        Note that the total free holes and electrons may also be used for a &quot;shallow donor/acceptor&quot;</span>
<span class="sd">               correction with specified band shifts:</span>
<span class="sd">                +num_elec_cbm * Delta E_CBM (or -num_hole_vbm * Delta E_VBM)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bf_corr</span> <span class="o">=</span> <span class="mf">0.</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;potalign&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">potalign</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;num_hole_vbm&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;num_elec_cbm&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mf">0.</span>

        <span class="n">core_occupation_value</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">eigenvalues</span><span class="o">.</span><span class="n">values</span><span class="p">())[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>  <span class="c1"># get occupation of a core eigenvalue</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">eigenvalues</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># needed because occupation of non-spin calcs is sometimes still 1... should be 2</span>
            <span class="n">spinfctr</span> <span class="o">=</span> <span class="mf">2.</span> <span class="k">if</span> <span class="n">core_occupation_value</span> <span class="o">==</span> <span class="mf">1.</span> <span class="k">else</span> <span class="mf">1.</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">eigenvalues</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="n">spinfctr</span> <span class="o">=</span> <span class="mf">1.</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Eigenvalue keys greater than 2&quot;</span><span class="p">)</span>

        <span class="c1"># for tracking mid gap states...</span>
        <span class="n">shifted_cbm</span> <span class="o">=</span> <span class="n">potalign</span> <span class="o">+</span> <span class="n">cbm</span>  <span class="c1"># shift cbm with potential alignment</span>
        <span class="n">shifted_vbm</span> <span class="o">=</span> <span class="n">potalign</span> <span class="o">+</span> <span class="n">vbm</span>  <span class="c1"># shift vbm with potential alignment</span>

        <span class="k">for</span> <span class="n">spinset</span> <span class="ow">in</span> <span class="n">eigenvalues</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">kptset</span><span class="p">,</span> <span class="n">weight</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">spinset</span><span class="p">,</span> <span class="n">kpoint_weights</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">eig</span><span class="p">,</span> <span class="n">occu</span> <span class="ow">in</span> <span class="n">kptset</span><span class="p">:</span>  <span class="c1"># eig is eigenvalue and occu is occupation</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">occu</span> <span class="ow">and</span> <span class="p">(</span><span class="n">eig</span> <span class="o">&gt;</span> <span class="n">shifted_cbm</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolution</span><span class="p">)):</span>  <span class="c1"># donor MB correction</span>
                        <span class="n">bf_corr</span> <span class="o">+=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">spinfctr</span> <span class="o">*</span> <span class="n">occu</span> <span class="o">*</span> <span class="p">(</span><span class="n">eig</span> <span class="o">-</span> <span class="n">shifted_cbm</span><span class="p">)</span>  <span class="c1"># &quot;move the electrons down&quot;</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;num_elec_cbm&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">spinfctr</span> <span class="o">*</span> <span class="n">occu</span>
                    <span class="k">elif</span> <span class="p">(</span><span class="n">occu</span> <span class="o">!=</span> <span class="n">core_occupation_value</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span>
                            <span class="n">eig</span> <span class="o">&lt;=</span> <span class="n">shifted_vbm</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolution</span><span class="p">):</span>  <span class="c1"># acceptor MB correction</span>
                        <span class="n">bf_corr</span> <span class="o">+=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">spinfctr</span> <span class="o">*</span> <span class="p">(</span><span class="n">core_occupation_value</span> <span class="o">-</span> <span class="n">occu</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span>
                                <span class="n">shifted_vbm</span> <span class="o">-</span> <span class="n">eig</span><span class="p">)</span>  <span class="c1"># &quot;move the holes up&quot;</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;num_hole_vbm&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">weight</span> <span class="o">*</span> <span class="n">spinfctr</span> <span class="o">*</span> <span class="p">(</span><span class="n">core_occupation_value</span> <span class="o">-</span> <span class="n">occu</span><span class="p">)</span>

        <span class="n">bf_corr</span> <span class="o">*=</span> <span class="o">-</span><span class="mi">1</span>  <span class="c1"># need to take negative of this shift for energetic correction</span>

        <span class="k">return</span> <span class="n">bf_corr</span></div></div>


<div class="viewcode-block" id="BandEdgeShiftingCorrection"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.BandEdgeShiftingCorrection">[docs]</a><span class="k">class</span> <span class="nc">BandEdgeShiftingCorrection</span><span class="p">(</span><span class="n">DefectCorrection</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A class for BandEdgeShiftingCorrection class. Largely adapted from PyCDT code</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initializes the BandEdgeShiftingCorrection class</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;vbmshift&quot;</span><span class="p">:</span> <span class="mf">0.</span><span class="p">,</span>
            <span class="s2">&quot;cbmshift&quot;</span><span class="p">:</span> <span class="mf">0.</span><span class="p">,</span>
        <span class="p">}</span>

<div class="viewcode-block" id="BandEdgeShiftingCorrection.get_correction"><a class="viewcode-back" href="../../../../pymatgen.analysis.defects.corrections.html#pymatgen.analysis.defects.corrections.BandEdgeShiftingCorrection.get_correction">[docs]</a>    <span class="k">def</span> <span class="nf">get_correction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entry</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets the BandEdge correction for a defect entry</span>
<span class="sd">        Args:</span>
<span class="sd">            entry (DefectEntry): defect entry to compute BandFilling correction on.</span>
<span class="sd">                Requires some parameters in the DefectEntry to properly function:</span>
<span class="sd">                    hybrid_cbm (float)</span>
<span class="sd">                        CBM of HYBRID bulk calculation one wishes to shift to</span>

<span class="sd">                    hybrid_vbm (float)</span>
<span class="sd">                        VBM of HYBRID bulk calculation one wishes to shift to</span>

<span class="sd">                    cbm (float)</span>
<span class="sd">                        CBM of bulk calculation (or band structure calculation of bulk);</span>
<span class="sd">                        calculated on same level of theory as the defect</span>
<span class="sd">                        (ex. GGA defects -&gt; requires GGA cbm)</span>

<span class="sd">                    vbm (float)</span>
<span class="sd">                        VBM of bulk calculation (or band structure calculation of bulk);</span>
<span class="sd">                        calculated on same level of theory as the defect</span>
<span class="sd">                        (ex. GGA defects -&gt; requires GGA vbm)</span>
<span class="sd">        Returns:</span>
<span class="sd">            BandfillingCorrection value as a dictionary</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">hybrid_cbm</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;hybrid_cbm&quot;</span><span class="p">]</span>
        <span class="n">hybrid_vbm</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;hybrid_vbm&quot;</span><span class="p">]</span>
        <span class="n">vbm</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;vbm&quot;</span><span class="p">]</span>
        <span class="n">cbm</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;cbm&quot;</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;vbmshift&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">hybrid_vbm</span> <span class="o">-</span> <span class="n">vbm</span>  <span class="c1"># note vbmshift has UPWARD as positive convention</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;cbmshift&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">hybrid_cbm</span> <span class="o">-</span> <span class="n">cbm</span>  <span class="c1"># note cbmshift has UPWARD as positive convention</span>

        <span class="n">charge</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">charge</span>
        <span class="n">bandedgeshifting_correction</span> <span class="o">=</span> <span class="n">charge</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s2">&quot;vbmshift&quot;</span><span class="p">]</span>
        <span class="n">entry</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s2">&quot;bandshift_meta&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">metadata</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;bandedgeshifting_correction&quot;</span><span class="p">:</span> <span class="n">bandedgeshifting_correction</span>
        <span class="p">}</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.defects.corrections</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>